package com.hermes.action.json;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.hermes.biz.common.model.comment.UserCommentDisplayUnitDO;
import com.hermes.biz.common.model.result.UserCommentResultDO;
import com.hermes.biz.common.model.result.UserResultDO;
import com.hermes.biz.common.model.user.UserCommentDO;
import com.hermes.biz.common.util.string.StringUtil;
import com.hermes.biz.service.item.ItemService;
import com.hermes.biz.service.user.UserCommentService;
import com.hermes.biz.service.user.UserService;
import com.hermes.common.action.AsynBaseAction;
import com.hermes.common.util.LoginHelperUtil;
import com.hermes.common.util.ParameterParserUtil;

/**
 * 
 * Publish-Comment-For-Detail
 * 
 */
public class AsynPublishUserComment  extends AsynBaseAction{

	private static final long serialVersionUID = -2631768137536616914L;

	private Logger log = Logger.getLogger(AsynPublishUserComment.class);
	
	private UserCommentService userCommentService;
	
	private ItemService itemService;
	
	private UserService userService;

	private String constent ;
	
	public UserCommentDisplayUnitDO userCommentDO;

	@Override
	public String execute() {
		
		String comment = ParameterParserUtil.getParameter("content");
		String itemId = ParameterParserUtil.getParameter("itemId");
		String msgType = ParameterParserUtil.getParameter("type");
		String originId= ParameterParserUtil.getParameter("fromId");
		
		long itemIdNum=0;
		long userId=7;
		long originIdNum=0;
		int msgTypeNum=0;
		
		if(StringUtil.isNotBlank(itemId)&&StringUtil.isNumOnly(itemId)){
			itemIdNum = Long.parseLong(itemId);
		}
		if(StringUtil.isNotBlank(originId)&&StringUtil.isNumOnly(originId)){
			originIdNum = Long.parseLong(originId);
		}
		if(StringUtil.isNotBlank(msgType)&&StringUtil.isNumOnly(msgType)){
			msgTypeNum = Integer.parseInt(msgType);
		}
		
		HttpServletRequest request = ServletActionContext.getRequest();
		LoginHelperUtil loginHelper = new LoginHelperUtil(request);
		if(loginHelper.hasLogined()){
			log.error("user-log-in-report-in-detail");
			String username = loginHelper.getCurrentUserName();
			UserResultDO userResultDO = userService.getUserInfoByUserName(username);
			if(userResultDO.isSuccess()&& userResultDO.getModule()!=null){
				userId = userResultDO.getModule().getUserId();
			}
		}
	
		log.error("user-comment-content:"+comment);

		UserCommentDO	commentDO = new UserCommentDO();
		commentDO.setComment(comment);
		commentDO.setCreateUserId(userId);
		commentDO.setItemId(itemIdNum);
		if(msgTypeNum>0 && originIdNum>0){
			commentDO.setType(msgTypeNum);
			commentDO.setOriginalCommentId(originIdNum);
			UserCommentResultDO ucrDO = userCommentService.getUserCommentInfoByCommentId(originIdNum);
			if(ucrDO.isSuccess()){
				commentDO.setOriginalUserId(ucrDO.getModule().getCreateUserId());
			}
		}
		else{
			commentDO.setType(0);
			commentDO.setOriginalCommentId(0);
			commentDO.setOriginalUserId(userId);
		}
		this.userCommentService.saveUserCommentInfo(commentDO);
		
		UserResultDO userDO = userService.getUserInfoByUserId(commentDO.getCreateUserId());
		userCommentDO = new UserCommentDisplayUnitDO();
		userCommentDO.setComment(comment);
		userCommentDO.setHeadPic(userDO.getModule().getHeadPic());
		userCommentDO.setItemId(itemIdNum);
		userCommentDO.setUserId(userDO.getModule().getUserId());
		userCommentDO.setUserName(userDO.getModule().getUserName());
		
		constent =  comment;
		 
		return SUCCESS;
	}

	public UserService getUserService() {
		return userService;
	}


	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public ItemService getItemService() {
		return itemService;
	}


	public void setItemService(ItemService itemService) {
		this.itemService = itemService;
	}


	public String getConstent() {
		return constent;
	}

	public void setConstent(String constent) {
		this.constent = constent;
	}
	public UserCommentDisplayUnitDO getUserCommentDO() {
		return userCommentDO;
	}

	public void setUserCommentDO(UserCommentDisplayUnitDO userCommentDO) {
		this.userCommentDO = userCommentDO;
	}

	public UserCommentService getUserCommentService() {
		return userCommentService;
	}

	public void setUserCommentService(UserCommentService userCommentService) {
		this.userCommentService = userCommentService;
	}
}
